//
//  AppDelegate.m
//  SAP
//
//  Created by 中矿科技有限公司 on 17/9/8.
//  Copyright © 2017年 中矿科技有限公司. All rights reserved.
//

#import "AppDelegate.h"
#import "SATabViewController.h"
#import "MMDrawerController.h"
#import "LeftSideDrawerViewController.h"
#import "SANavigationViewController.h"
#import "LoginViewController.h"
#import "Reachability.h"


@interface AppDelegate ()
@property (nonatomic,strong) MMDrawerController * drawerController;

@property (nonatomic) Reachability *hostReach;



@end


@implementation AppDelegate
{
    MBProgressHUD * HUD;
}

+ (instancetype)sharedAppDelegate
{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    

    
    

    
    [self configAppLaunch];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self enterLoginUI];
    [self.window makeKeyAndVisible];
    [self keyboardManager];
    return YES;
}


- (void)configAppLaunch
{
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(reachabilityChanged:)
     
                                                 name:kReachabilityChangedNotification
     
                                               object:nil];
    self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
    
    [self.hostReach  startNotifier];
}

- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]] || [curReach isKindOfClass:NSClassFromString(@"WXReachability")]);
    [self updateInterfaceWithReachability:curReach];
}

- (void)updateInterfaceWithReachability: (Reachability*)reachability
{
    //对连接改变做出响应的处理动作。
    
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    // BOOL connectionRequired = [reachability connectionRequired];
    
    if (netStatus== NotReachable) { //没有连接到网络就弹出提实况
        if (!HUD)HUD = [[MBProgressHUD alloc] initWithView:self.window.rootViewController.view];
        [self.window addSubview:HUD];
        HUD.label.text = @"当前网络异常,请检查您的网络";
        HUD.mode = MBProgressHUDModeText;
       // HUD.yOffset =[[UIScreen mainScreen] bounds].size.height/2-50;
        HUD.offset =CGPointMake(0,[[UIScreen mainScreen] bounds].size.height/2-100 );
        [HUD showAnimated:YES];
        [HUD hideAnimated:YES afterDelay:4];
    }
}


-(void)keyboardManager{
    // 设置键盘监听管理
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    
    // IQKeyboardReturnKeyHandler *returnKeyHandler =[[IQKeyboardReturnKeyHandler alloc] initWithViewController:self.window.rootViewController];
    
    manager.enable = YES; // 控制整个功能是否启用。
    
    manager.shouldResignOnTouchOutside =YES; // 控制点击背景是否收起键盘
    
    manager.shouldToolbarUsesTextFieldTintColor =YES; // 控制键盘上的工具条文字颜色是否用户自定义
    
    manager.enableAutoToolbar =NO ; // 控制是否显示键盘上的工具条
    
    manager.toolbarDoneBarButtonItemText = @"完成";
    
    manager.shouldShowTextFieldPlaceholder = NO;
    
    manager.placeholderFont = [UIFont systemFontOfSize:16.0];
    
    manager.toolbarManageBehaviour =IQAutoToolbarByTag;
}


// 进入登录界面
// 用户可重写
- (void)enterLoginUI
{
    
    LoginViewController *vc = [[LoginViewController alloc] init];
    self.window.rootViewController = vc;
    
}
// 进入主界面逻辑
- (void)enterMainUI{
    UIViewController * leftSideDrawerViewController = [[LeftSideDrawerViewController alloc] init];
    
    UIViewController * centerViewController = [[SATabViewController alloc] init];
    
    
    [centerViewController setRestorationIdentifier:@"MMExampleCenterNavigationControllerRestorationKey"];
    
    UINavigationController * leftSideNavController = [[UINavigationController alloc] initWithRootViewController:leftSideDrawerViewController];
    [leftSideNavController setRestorationIdentifier:@"MMExampleLeftNavigationControllerRestorationKey"];
    self.drawerController = [[MMDrawerController alloc]
                             initWithCenterViewController:centerViewController
                             leftDrawerViewController:leftSideNavController
                             ];
    //设置抽屉的宽度
    _drawerController.maximumLeftDrawerWidth =280;
    [self.drawerController setShowsShadow:NO];
    [self.drawerController setRestorationIdentifier:@"MMDrawer"];
    [self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    

    self.window.rootViewController = self.drawerController;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
